package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.ICheckInMeetingDao;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.CheckInImplement.DataSource;
import com.eling.elcms.travel.model.CheckInMeeting;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import com.eling.elcms.travel.service.ICheckInMeetingManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CheckInMeetingManagerImpl extends GenericManagerImpl<CheckInMeeting, Long> implements ICheckInMeetingManager{

	ICheckInMeetingDao meetingDao;

	@Autowired
	private ICheckInImplementManager cIImplementM;
	
	@Autowired
	public void setDao(ICheckInMeetingDao dao) {
        this.dao = dao;
        this.meetingDao = dao;
    }
	
	@Override
	public CheckInMeeting confirm(CheckInMeeting dinner){
		
		dinner.setStatus(CheckStatus.Pended);
		dinner = save(dinner);
		
		// 设置入住准备落实单总表的已落实状态
		if (dinner.getStatus() != null && dinner.getStatus() == CheckStatus.Pended){
			cIImplementM.setCiiCheckStatusConfirm(dinner.getCheckInImplement());
		}
		
		return dinner;
	}

	@Override
	public List<CheckInMeeting> queryAll(CheckInMeeting dinner, Date startDate, Date endDate) {
		List<CheckInMeeting> returnList = new ArrayList<>();
		
		//查询机构、联盟团的入住准备车辆落实单
		PropertyUtils.setProperty(dinner, "checkInImplement.groupReserve.checkInDate", startDate);
		PropertyUtils.setProperty(dinner, "checkInImplement.groupReserve.checkInDateEnd", endDate);
		List<CheckInMeeting> cidOneList =query(dinner);
		if(!cidOneList.isEmpty()){
			returnList.addAll(cidOneList);
		}
		
		//查询散客团的入住准备车辆落实单
		PropertyUtils.setProperty(dinner, "checkInImplement.groupReserve", null);
		PropertyUtils.setProperty(dinner, "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate", startDate);
		PropertyUtils.setProperty(dinner, "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDateEnd", endDate);
		List<CheckInMeeting> cidTwoList = query(dinner);
		if(!cidTwoList.isEmpty()){
			returnList.addAll(cidTwoList);
		}
		
		Collections.sort(returnList,new Comparator<CheckInMeeting>() {
			public int compare(CheckInMeeting o1, CheckInMeeting o2) {
				Date date1 = null;
				Date date2 = null;
				//散客
				if(o1.getCheckInImplement().getDataSource()==DataSource.NonMember){
					date1 = o1.getCheckInImplement().getNonMemGroupReserve().getNonMemberGroup().getCheckInDate();
				}else{//老人团
					date1 = o1.getCheckInImplement().getGroupReserve().getCheckInDate();
				}
				//散客
				if(o2.getCheckInImplement().getDataSource()==DataSource.NonMember){
					date2 = o2.getCheckInImplement().getNonMemGroupReserve().getNonMemberGroup().getCheckInDate();
				}else{//老人团
					date2 = o2.getCheckInImplement().getGroupReserve().getCheckInDate();
				}
				return date1.compareTo(date2);
			};
		});
		
		return returnList;
	}
}
